﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace it_security_project.App_Code
{
    public static class PageExtensions
    {
        public static void AddErrorMessage(this Page page, string message, string validationGroup)
        {
            var validator = new CustomValidator
            {
                IsValid = false,
                ErrorMessage = message,
                ValidationGroup = validationGroup
            };

            page.Validators.Add(validator);
        }

        public static void AddErrorMessage(this Page page, IDataErrorInfo obj, string validationGroup)
        {
            obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)
                .Where(p => String.IsNullOrWhiteSpace(obj[p.Name]))
                .Select(p => p.Name)
                .ToList()
                .ForEach(propertyName => PageExtensions.AddErrorMessage(page, obj[propertyName], validationGroup));
        }
    }
}